const findSmallest = (array) => {
    let smallestIndex = 0;
    let smallestItem;
    // if(array.length == 1)
    //     return 0;
    // 当剩余最后一个元素时，它就是数组里的最小值
    // 此时 i == 1 == array.length
    for(let i = 1; i < array.length; i ++){
        smallestItem = array[smallestIndex];
        if(array[i] < smallestItem){
            smallestIndex = i;
        }
    }
    return smallestIndex;
};

const selectionSort = (array) => {
    // 复制一份要排序的数组，否则会在取出最小值后，改变了原来的数组array
    const sortingArr = [...array];
    const newArr = [];
    while(sortingArr.length > 0){
        const smallestIndex = findSmallest(sortingArr);
        // splice 返回[被取出元素]，同时改变了原array
        newArr.push(sortingArr.splice(smallestIndex,1)[0]);
    }
    return newArr;
}

selectionSort([5,7,1,34,2,100]);